package com.hzqy.web.vo;

import java.io.Serializable;
import java.util.Map;

import javax.servlet.ServletContext;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSONObject;
import com.hzqy.service.wchat.WechatWXService;

public class WxAccessTokenVo implements Serializable{
	
	private static final long serialVersionUID = -3573855454223366699L;

	private final static Logger LOG = LoggerFactory.getLogger(WxAccessTokenVo.class);
	
	private String wx_token;
	private String wx_appid;
	private String wx_secret;
	
	private WechatWXService wechatWXService;
	
	private long last_access_token_time;
	
	private int type; // 1.普通access_token 2.jsapi access_token(jsapi_ticket)
	
	private String access_token;
	private long expires_in;
	
	
	public WxAccessTokenVo(WechatWXService wechatWXService, String wx_token, String wx_appid, String wx_secret, int type) {
		this.wechatWXService = wechatWXService;
		this.wx_token = wx_token;
		this.wx_appid = wx_appid;
		this.wx_secret = wx_secret;
		this.type = type;
		this.refreshAccessToken(true);
	}
	
	public static final String application_access_token_key = "wx_access_token";
	public static final String application_js_access_token_key = "wx_js_access_token";
	public static ServletContext application;
	
	public static final String getAccessToken(boolean useCache, String key) {
		
		WxAccessTokenVo wxAccessTokenVo = getAccessTokenVo(key);
		if(wxAccessTokenVo != null) {
			return wxAccessTokenVo.getAccess_token(useCache);
		}
		return "";
	}
	
	public static final WxAccessTokenVo getAccessTokenVo(String key) {
		return (WxAccessTokenVo) application.getAttribute(key);
	}
	
	public static final String getAccessToken(boolean useCache) {
		return getAccessToken(useCache, application_access_token_key);
	}
	
	public static final String getJsAccessToken(boolean useCache) {
		return getAccessToken(useCache, application_js_access_token_key);
	}
	
	public void refreshAccessToken(boolean useCache) {
		if(this.type == 1) {
			if(!(expires_in > 0 && StringUtils.isNotEmpty(access_token) && last_access_token_time > 0 
					&& (last_access_token_time + expires_in * 1000 / 2 ) > System.currentTimeMillis()) || !useCache) {
				LOG.debug("刷新/获取微信access_token");
				Map<String, String> resultMap = wechatWXService.access_token_I();
				LOG.debug("刷新/获取微信access_token, 获取结果：" + resultMap);
				if ("1".equals(resultMap.get("success"))) {
					String result = resultMap.get("msg");
					JSONObject jsonObj = JSONObject.parseObject(result);
					String access_token = jsonObj.getString("access_token");
					long expires_in = jsonObj.getLongValue("expires_in");
					this.setAccess_token(access_token);
					this.setExpires_in(expires_in);
					last_access_token_time = System.currentTimeMillis();
					LOG.debug("获取微信access_token成功:" + this);
				}
			}
		} else if(this.type == 2) {
			if(!(expires_in > 0 && StringUtils.isNotEmpty(access_token) && last_access_token_time > 0 
					&& (last_access_token_time + expires_in * 1000 / 2 ) > System.currentTimeMillis()) || !useCache) {
				LOG.debug("刷新/获取微信jsapi access_token");
				Map<String, String> resultMap = wechatWXService.jsapi_ticket();
				LOG.debug("刷新/获取微信jsapi access_token, 获取结果：" + resultMap);
				if ("1".equals(resultMap.get("success"))) {
					String result = resultMap.get("msg");
					JSONObject jsonObj = JSONObject.parseObject(result);
					if("0".equals(jsonObj.getString("errcode"))) {
						String access_token = jsonObj.getString("ticket");
						long expires_in = jsonObj.getLongValue("expires_in");
						this.setAccess_token(access_token);
						this.setExpires_in(expires_in);
						last_access_token_time = System.currentTimeMillis();
						LOG.debug("获取微信jsapi access_token成功 :" + this);
						
					}
				}
			}
		}
	}
	
	
	public String getWx_token() {
		return wx_token;
	}
	public void setWx_token(String wx_token) {
		this.wx_token = wx_token;
	}
	public String getWx_appid() {
		return wx_appid;
	}
	public void setWx_appid(String wx_appid) {
		this.wx_appid = wx_appid;
	}
	public String getWx_secret() {
		return wx_secret;
	}
	public void setWx_secret(String wx_secret) {
		this.wx_secret = wx_secret;
	}
	public String getAccess_token() {
		refreshAccessToken(true);
		return access_token;
	}
	public String getAccess_token(boolean useCache) {
		refreshAccessToken(useCache);
		return access_token;
	}
	public void setAccess_token(String access_token) {
		this.access_token = access_token;
	}
	public long getExpires_in() {
		return expires_in;
	}
	public void setExpires_in(long expires_in) {
		this.expires_in = expires_in;
	}

	@Override
	public String toString() {
		return "WxAccessTokenVo [wx_token=" + wx_token + ", wx_appid=" + wx_appid + ", wx_secret=" + wx_secret + ", wechatWXService="
				+ wechatWXService + ", last_access_token_time=" + last_access_token_time + ", type=" + type + ", access_token=" + access_token
				+ ", expires_in=" + expires_in + "]";
	}
	
}
